package com.cater.sys.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cater.annotation.SystemControllerLog;
import com.cater.common.BaseEntity;
import com.cater.common.PageResult;
import com.cater.query.CommonQuery;
import com.cater.sys.entity.Dictionarydata;
import com.cater.sys.service.impl.DictionarydataServiceImpl;

/**
 * 字典
 * 
 * @author ShenDong
 *
 */
@Controller
@RequestMapping("/dictionarydata")
public class DictionarydataController {

	@Autowired
	DictionarydataServiceImpl service;

	/**
	 * 打开字典
	 * 
	 * @return
	 */
	@RequestMapping(value = "/list")
	public String list(Model model) {
		List<Dictionarydata> dictionarydatas = service.getGroupByType();
		model.addAttribute("dictionarydatas", dictionarydatas);
		return "sys/dictionarydata/dictionarydata_list";
	}

	@ResponseBody
	@RequestMapping(value = "/getAll")
	@SystemControllerLog(description = "查看字典")
	public List<Dictionarydata> getAll(HttpServletRequest request) {
		return service.getAll();
	}

	@RequestMapping("/listData")
	@ResponseBody
	@SystemControllerLog(description = "查看字典")
	public PageResult<Dictionarydata> getAll(CommonQuery query, HttpServletRequest request) {
		PageResult<Dictionarydata> result = service.findPageResult(query);
		return result;
	}

	@ResponseBody
	@RequestMapping(value = "/getDictionarydataBypid")
	@SystemControllerLog(description = "根据字典ID查询字典")
	public JSONObject getDictionarydataBypid(String nodeid, CommonQuery query) {
		JSONObject json = new JSONObject();
		Dictionarydata dictionarydata = service.getById(nodeid);
		json.put("dictionarydata", dictionarydata);
		return json;
	}

	@RequestMapping("/addView")
	public String addView(Model model, CommonQuery query) {
		return "sys/dictionarydata/dictionarydata_add";
	}

	@ResponseBody
	@RequestMapping("/add")
	@SystemControllerLog(description = "新增字典")
	public JSONObject add(Dictionarydata dictionarydata) {
		JSONObject json = new JSONObject();
		try {
			service.save(dictionarydata);
			json.put("code", 200);
			json.put("message", "添加成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "添加失败");
		}
		return json;
	}

	@ResponseBody
	@RequestMapping("/delete")
	@SystemControllerLog(description = "删除字典")
	public JSONObject delete(String id, CommonQuery query) {
		JSONObject json = new JSONObject();
		try {
			service.delete(id);
			json.put("code", 200);
			json.put("message", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "删除失败");
		}
		return json;
	}

	@RequestMapping("/showedit")
	public String showeditDictionarydata(String id, Model model, CommonQuery query) {
		Dictionarydata dictionarydata = service.getById(id);
		model.addAttribute("dictionarydata", dictionarydata);
		return "sys/dictionarydata/dictionarydata_edit";
	}

	@RequestMapping("/addValueView")
	public String addValueView(String id, Model model, CommonQuery query) {
		Dictionarydata dictionarydata = service.getById(id);
		model.addAttribute("dictionarydata", dictionarydata);
		return "sys/dictionarydata/add_value";
	}

	@ResponseBody
	@RequestMapping("/editdictionarydata")
	@SystemControllerLog(description = "修改字典")
	public JSONObject editDictionarydata(Dictionarydata dictionarydata) {
		JSONObject json = new JSONObject();
		try {
			service.update(dictionarydata);
			json.put("code", 200);
			json.put("message", "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "修改失败");
		}
		return json;
	}

	/**
	 * @根据字典类型type获取数据
	 * @param types
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getJsonByType")
	public JSONObject getJsonByType(String types) {
		JSONObject json = new JSONObject();
		List<Dictionarydata> dictionarydatas = null;
		try {
			dictionarydatas = service.getDataByType(types);
			json.put("success", "数据获取成功");
			json.put("list", dictionarydatas);
		} catch (Exception e) {
			json.put("fail", "数据获取失败");
		}
		return json;
	}

	@RequestMapping("getLable")
	@ResponseBody
	public String getByname(String values, String types) {
		Dictionarydata dictionarydata = service.getByTypeValue(values, types);
		return dictionarydata.getValueName();
	}
}
